package cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment3Refactoring.seeder;

import java.util.Random;

import cn.edu.nju.software.zp13.client.assignment3.xmlNodeEntity.util.ScoreType;
import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment3Refactoring.bean.CourseScore;
import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment3Refactoring.bean.Score;


/**
 * This class is to generate random courses and random socres
 * 
 * @author Ivan
 *
 */
public class CourseScoreGenerator {
	/**
	 * return several courseScores
	 * 
	 * @param numOfCourses
	 *            the number of courses your need to generate
	 * @return
	 */
	public static CourseScore[] run(int numOfCourses, String studentId) {
		CourseScore[] css = new CourseScore[numOfCourses * 3];
		int[] scores = generateScores(numOfCourses);
		String[] courseIds = generateCourseId(numOfCourses);
		for (int i = 0; i < css.length; i++) {
			CourseScore cs = new CourseScore();
			cs.setCourseId(courseIds[i / 3]);
			cs.setScore(new Score(studentId, scores[i]));
			cs.setSt(getScoreType(i));
			css[i] = cs;
		}
		return css;
	}

	private static int[] generateScores(int numOfCourses) {
		int[] scores = new int[3 * numOfCourses];
		Random r = new Random();
		for (int i = 0; i < scores.length; i++) {
			scores[i] = r.nextInt(100);
		}
		return scores;
	}

	private static String[] generateCourseId(int numOfCourses) {
		String[] courseId = new String[numOfCourses];
		for (int i = 0; i < numOfCourses; i++) {
			courseId[i] = 100001 + i + "";
		}
		return courseId;
	}

	private static ScoreType getScoreType(int i) {
		ScoreType st = ScoreType.平时成绩;
		switch (i % 3) {
		case 0:
			st = ScoreType.平时成绩;
			break;
		case 1:
			st = ScoreType.期末成绩;
			break;
		case 2:
			st = ScoreType.总评成绩;
			break;
		default:
			break;
		}
		return st;
	}

}
